package com.steven.ourcontest.information.view

import android.view.KeyEvent
import android.widget.TextView.OnEditorActionListener
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.information.adapter.SearchSchoolAdapter
import com.steven.ourcontest.information.viewmodel.IdentifyViewModel
import com.steven.ourcontest.utils.Identity
import kotlinx.android.synthetic.main.activity_select_school.*


class SelectSchoolActivity : BaseActivity() {
    private val searchSchoolAdapter by lazy {
        SearchSchoolAdapter(this, identity!!)
    }
    private var identity: Identity? = null
    private val identifyViewModel: IdentifyViewModel by viewModels()
    override fun getViewId(): Int? {
        return R.layout.activity_select_school
    }

    override fun initView() {
        identity = intent.getSerializableExtra("identity") as? Identity?
        if (identity == null) {
            finish()
            return
        }
        rv_school_initials?.adapter = searchSchoolAdapter
        identifyViewModel.schoolList.observe(this, Observer {
            it?.schoolInfo?.let { it1 ->
                searchSchoolAdapter.submitData(it1)
            }
        })
        et_school?.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
            //当actionId == XX_SEND 或者 XX_DONE时都触发
            //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
            //注意，这是一定要判断event != null。因为在某些输入法上会返回null。
            if (event != null && KeyEvent.KEYCODE_ENTER == event.keyCode && KeyEvent.ACTION_DOWN == event.action) {
                val editText = et_school.text.toString()
                if (editText != "") {
                    identifyViewModel.querySchoolList(editText)
                }
                return@OnEditorActionListener true
            }
            false
        })
    }

}